package com.cogent.dynamicform.expression.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.facade.BussinessDaoFacade;
import com.cogent.dynamicform.domain.BusControlDomain;
import com.cogent.dynamicform.domain.FormDomain;
import com.cogent.dynamicform.expression.IExpression;
import com.cogent.dynamicform.facade.IExpressionFacade;
import com.cogent.dynamicform.facade.impl.ExpressionFacadeImpl;

public class ExecuteSQLExpression implements IExpression {
	
	public boolean doExpression(BusControlDomain busControlDomain, BussinessDaoFacade bussinessFacade, FormDomain formDomain, HashMap valMap) {
		String[] temp = busControlDomain.getInfo().split("\\|");
		boolean flag = true;
		if(!StringUtil.isEmpty(busControlDomain.getPrerequisites())) {
			IExpressionFacade iexpressionFacade = new ExpressionFacadeImpl();
			flag = iexpressionFacade.doJudge(busControlDomain.getPrerequisites(), valMap);
		}
		if(flag) {
			List queryList = new ArrayList();
			String[] curtemps = temp[1].split(";");
			for(int i=0; i<curtemps.length; i++) {
				String[] mapfiled = curtemps[i].split("~");
				String curField = mapfiled[0];
				String curmapField = curField;
				if(mapfiled.length > 1)
					curmapField = mapfiled[1];
				queryList.add(new QueryParamDomain(temp[2], curmapField, curmapField, MapUtil.getStringValue(valMap, curField)));
			}
			
			bussinessFacade.executeSql(temp[0], queryList);
		}
		
		return flag;
	}

}
